#include "addfrienddialog.h"
#include <QGridLayout>
#include <QScrollArea>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include "debug.h"
#include "model/datacenter.h"

using namespace model;

FriendResultItem::FriendResultItem(const UserInfo &userInfo)
    :userInfo(userInfo)
{
    // 1设置自身基本属性
    this->setFixedHeight(70);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 2 创建布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(0);
    layout->setVerticalSpacing(0);
    layout->setHorizontalSpacing(10);
    layout->setContentsMargins(0, 0, 20, 0);
    this->setLayout(layout);

    // 3 创建头像
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(50, 50);
    avatarBtn->setIconSize(QSize(50, 50));
    avatarBtn->setIcon(userInfo.avatar);

    // 4 创建用户昵称
    QLabel* nameLabel = new QLabel();
    // 整个Item高度是70, 昵称和个性签名各占一半
    nameLabel->setFixedHeight(35);
    nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    nameLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    nameLabel->setText(userInfo.nickname);
    nameLabel->setStyleSheet("QLabel { font-size: 16px; font-weight: 700;}");

    // 5 创建个性签名
    QLabel* descLabel = new QLabel();
    descLabel->setFixedHeight(35);
    descLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    descLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    descLabel->setStyleSheet("QLabel { font-size: 14px; }");
    descLabel->setText(userInfo.description);

    // 6 创建添加好友按钮
    addBtn = new QPushButton();
    addBtn->setFixedSize(100, 40);
    addBtn->setText("添加好友");
    QString btnStyle = "QPushButton { border: none; background-color: rgb(137, 217, 97); "
                       "color: rgb(255, 255, 255); border-radius: 10px;}";
    btnStyle += "QPushButton:pressed{ background-color: rgb(200, 200, 200);}";
    addBtn->setStyleSheet(btnStyle);

    // 7 把上诉内容添加到布局管理器中
    layout->addWidget(avatarBtn, 0 ,0, 2, 1);
    layout->addWidget(nameLabel, 0, 1, 1, 1);
    layout->addWidget(descLabel, 1, 1, 1, 1);
    layout->addWidget(addBtn, 0, 2, 2, 1);

    // 8.连接信号槽
    connect(addBtn, &QPushButton::clicked, this, &FriendResultItem::clickAddBtn);

}

void FriendResultItem::clickAddBtn()
{
    //发送好友申请
    DataCenter* dataCenter  = DataCenter::getInstance();
    //申请好友的逻辑编写过了此处只需要调用之前代码
    dataCenter->addFriendApplyAsync(this->userInfo.userId);

    //2. 设置按钮为禁用状态
    addBtn->setEnabled(false);
    addBtn->setText("已申请");
    addBtn->setStyleSheet("QPushButton { border:none; color: rgb(255, 255, 255); background-color : rgb(200, 200, 200), \
border-radius : 10px; }");

}



AddFriendDialog::AddFriendDialog(QWidget *parent)
    :QDialog(parent)
{
    // 1 设置基本属性
    this->setFixedSize(500, 500);
    this->setWindowTitle("添加好友");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255);}");
    this->setAttribute(Qt::WA_DeleteOnClose);//关闭窗口自动释放


    //2  添加布局管理器
    layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(20, 20, 20, 20);
    this->setLayout(layout);

    //3 创建搜索框
    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(50);
    searchEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QString style = "QLineEdit { border: none; border-radius: 10px; font-size: 16px; background-color: rgb(240, 240, 240);"
                    "padding-left: 5px;}";
    searchEdit->setStyleSheet(style);
    searchEdit->setPlaceholderText("按手机号/用户序号/昵称搜索");
    layout->addWidget(searchEdit, 0, 0, 1, 8);

    // 4 创建搜索按钮
    QPushButton* searchBtn = new QPushButton();
    searchBtn->setFixedSize(50, 50);
    searchBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    searchBtn->setIconSize(QSize(30, 30));
    searchBtn->setIcon(QIcon(":/resource/image/search.png"));
    QString btnStyle = "QPushButton { border: none; background-color: rgb(240, 240,240); border-radius: 10px; }";

    //设置按钮 pressed和hover要先设置 hover后设置pressed
    btnStyle += "QPushButton:hover { background-color: rgb(220, 220, 220);}";
    btnStyle += "QPushButton:pressed { background-color: rgb(200, 200, 200);} ";

    searchBtn->setStyleSheet(btnStyle);
    layout->addWidget(searchBtn, 0, 8, 1, 1);

    // 5 添加滚动区域
    initResultArea();

    // 构造假数据验证界面效果
#if TEST_UI
    QIcon avatar(":/resource/image/defaultAvatar.png");
    for (int i = 0;  i < 20; ++i)
    {
        //new 出这个对象再, 再往addResult中添加, FriendResultItem 中持有 UserInfo的const 引用, 需要确保引用对象存在
        UserInfo* userInfo = new UserInfo();
        userInfo->userId = QString::number(i + 1000);
        userInfo->nickname = "誓约" + QString::number(i);
        userInfo->description = "好好学习,天天向上";
        userInfo->avatar = avatar;
        this->addResult(*userInfo);
    }
#endif

    // 6. 连接信号槽
    connect(searchBtn, &QPushButton::clicked, this, &AddFriendDialog::clickSearchBtn);

}

void AddFriendDialog::initResultArea()
{
    // 1 创建滚动区域对象
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    scrollArea->setWidgetResizable(true);
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0;}");
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; background-color: rgb(255, 255, 255);}");
    scrollArea->setStyleSheet("QScrollArea { border:none;}");

    layout->addWidget(scrollArea, 1, 0, 1, 9);

    // 2 创建QWdiget
    resultContainer = new QWidget();
    resultContainer->setObjectName("resultContainer");
    resultContainer->setStyleSheet("#resultContainer { background-color: rgb(255 ,255, 255);}");
    scrollArea->setWidget(resultContainer);

    // 给Widget里添加元素, 需要创建垂直布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(0, 0, 0, 0);

    resultContainer->setLayout(vlayout);

}

void AddFriendDialog::addResult(const UserInfo &userInfo)
{
    FriendResultItem* item = new FriendResultItem(userInfo);
    resultContainer->layout()->addWidget(item);

}

void AddFriendDialog::clear()
{
    //从后往前遍历
    QVBoxLayout* layout = dynamic_cast<QVBoxLayout*> (resultContainer->layout());
    //QLayout* layout = resultContainer->layout();
    for (int i = layout->count() - 1; i >= 0; --i)
    {
        QLayoutItem* layoutItem = layout->takeAt(i);
        if (layoutItem == nullptr || layoutItem->widget() == nullptr)
        {
            continue;
        }

        //删除其中元素
        delete layoutItem->widget();
    }
}

void AddFriendDialog::setSearchKey(const QString &searchKey)
{
    searchEdit->setText(searchKey);
}

void AddFriendDialog::clickSearchBtn()
{
    // 1. 获取输入框中的内容
    const QString& text = searchEdit->text();
    if (text.isEmpty()) {
        return;
    }

    // 2.给服务器发起请求
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::searchUserDone, this, &AddFriendDialog::clickSearchBtnDone, Qt::UniqueConnection);
    dataCenter->searchUserAsync(text);
}

void AddFriendDialog::clickSearchBtnDone()
{
    // 1. 拿到dataCenter中的搜索结果
    DataCenter* dataCenter = DataCenter::getInstance();
    QList<UserInfo>* searchResult = dataCenter->getSearchUserResult();
    if (searchResult == nullptr) {
        return;
    }
    this->clear();
    for (const auto& u: *searchResult) {
        this->addResult(u);
    }
}




